<?php

namespace App\Http\Controllers\Fake;

use App\Http\Controllers\Controller;
use App\Models\FakeList;
use App\Models\Floor;
use App\Models\Institude_Info;
use App\Models\Inverstment;
use App\Models\Parking;
use App\Models\User;
use App\Traits\Trait103_108;
use App\Traits\Trait109_113;
use App\Traits\Trait114_116;
use App\Traits\Trait117_119;
use App\Traits\Trait120_123;
use App\Traits\Trait124_127;
use App\Traits\Trait128_129;
use App\Traits\Trait12_14;
use App\Traits\Trait130_131;
use App\Traits\Trait132_136;
use App\Traits\Trait137;
use App\Traits\Trait15_16;
use App\Traits\Trait17_22;
use App\Traits\Trait23;
use App\Traits\Trait24_25;
use App\Traits\Trait26_29;
use App\Traits\Trait30_33;
use App\Traits\Trait34_35;
use App\Traits\Trait36;
use App\Traits\Trait37_38;
use App\Traits\Trait39;
use App\Traits\Trait40_42;
use App\Traits\Trait43_44;
use App\Traits\Trait45_47;
use App\Traits\Trait48_49;
use App\Traits\Trait50_51;
use App\Traits\Trait52_55;
use App\Traits\Trait56_59;
use App\Traits\Trait60_63;
use App\Traits\Trait64_75;
use App\Traits\Trait76;
use App\Traits\Trait77_80;
use App\Traits\Trait81_83;
use App\Traits\Trait84_86;
use App\Traits\Trait87_90;
use App\Traits\Trait91_97;
use App\Traits\Trait98;
use App\Traits\Trait99_102;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class IndexController extends Controller
{
    use Trait12_14, Trait15_16, Trait17_22, Trait23, Trait24_25, Trait26_29, Trait30_33, Trait34_35, Trait36, Trait37_38, Trait39, Trait40_42;
    use Trait43_44, Trait45_47, Trait48_49, Trait50_51, Trait52_55, Trait56_59, Trait60_63, Trait64_75, Trait76, Trait77_80, Trait81_83;
    use Trait84_86, Trait87_90, Trait91_97, Trait98, Trait99_102, Trait103_108, Trait109_113, Trait114_116, Trait117_119, Trait120_123;
    use Trait124_127, Trait128_129, Trait130_131, Trait132_136, Trait137;

    public function search(Request $request)
    {
        $validate = $request->validate([
            'keyword' => 'nullable'
        ]);
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $validate['keyword']
        ];
        return response()->json($responseJson);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse 研究中心简介
     */
    public function techTitle(Request $request)
    {
        $data = Institude_Info::select(['title', 'description'])->first();
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $data
        ];
        return response()->json($responseJson);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse 消息通知
     */
    public function newNotice(Request $request)
    {
        $data = FakeList::select(['id', 'message'])->wherein('id', [44, 45])->get();
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $data
        ];
        return response()->json($responseJson);
    }

    /**
     * @param Request 安全运行时间
     * @return \Illuminate\Http\JsonResponse
     */
    public function safeTime(Request $request)
    {
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => [
                'day' => random_int(0,356),
                'hour' => random_int(0,23),
                'minute' => random_int(0,60)
            ]
        ];
        return response()->json($responseJson);
    }

    /**
     * @return void 今日和昨日访客
     */
    public function visitor()
    {
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => [
                'today' => random_int(0,100),
                'yesterday' => random_int(0,100),
            ]
        ];
        return response()->json($responseJson);
    }

    /**
     * @return void 访客周统计
     */
    public function visitorWeekStatistics()
    {
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => [
                [
                    "key" => '1',
                    "value" => random_int(0,100),
                ],
                [
                    "key" => '2',
                    "value" => random_int(0,100),
                ],
                [
                    "key" => '3',
                    "value" => random_int(0,100),
                ],
                [
                    "key" => '4',
                    "value" => random_int(0,100),
                ],
                [
                    "key" => '5',
                    "value" => random_int(0,100),
                ],
                [
                    "key" => '6',
                    "value" => random_int(0,100),
                ],
                [
                    "key" => '7',
                    "value" => random_int(0,100),
                ]
            ]
        ];
        return response()->json($responseJson);
    }

    /**
     * @return void 访客月统计
     */
    public function visitorMonthStatistics()
    {
        // 获取当前日期和月份天数
        $currentDay =  intval(date('j'));           // 当前的日期（几号）
        $daysInMonth = intval(date('t'));;  // 当前月份的总天数
        // 初始化 data 数组
        $data = [];
        // 循环生成每天的随机访客数
        for ($i = 1; $i <= $daysInMonth; $i++) {
            // 如果日期还未到，则设置为0，否则生成0到100之间的随机数
            if ($i > $currentDay) {
                $visit = 0;  // 今天之后的日期，访问量为0
            } else {
                $visit = random_int(0, 100);  // 今天之前和今天的日期生成随机数
            }
            $data[] = ['day' => $i, 'man' => $visit];

        }
        // 生成响应数据
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $data
        ];
        // 返回 JSON 响应
        return response()->json($responseJson);
    }

    /**
     * @return void 停车位使用状况
     */
    public function parkingStatistics()
    {

        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => [
                'B1' => [
                    'used_parking' => Parking::where(['floor_id' => -1, 'status' => 1])->count(),
                    'all_parking' => Parking::where(['floor_id' => -1])->count(),
                ],
                'B2' => [
                    'used_parking' => Parking::where(['floor_id' => -2, 'status' => 1])->count(),
                    'all_parking' => Parking::where(['floor_id' => -2])->count(),
                ],
            ]
        ];
        return response()->json($responseJson);
    }

    /**
     * @return void 充电桩使用状况
     */
    public function chargingStatistics()
    {
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => [
                'used_charging' => random_int(0, 130),
                'all_charging' => 130,
            ]
        ];
        return response()->json($responseJson);
    }

    /**
     * 招商合作意向接口
     * @return \Illuminate\Http\JsonResponse
     */
    public function investment(Request $request)
    {
        $validatedData = $request->validate([
            'page' => 'nullable|integer',
            'pageSize' => 'nullable|integer',
        ]);
        $pageSize = $validatedData['pageSize'] ?? 20; // 默认每页20条记录
        $users = Inverstment::paginate($pageSize);
        $data = [
            'page' => $users->currentPage(),
            'pageSize' => intval($users->perPage()),
            'rows' => $users->items(),
            'total' => $users->total(),
        ];
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $data
        ];
        return Response()->json($responseJson);
    }

}
